package com.weds.frame.auth.runner;

import com.weds.frame.auth.entity.ScBusinessOptApi;
import com.weds.frame.auth.mapper.ScBusinessOptApiAuthMapper;
import com.weds.frame.auth.mapper.ScBusinessOptAuthMapper;
import com.weds.frame.auth.service.CommUtil;
import com.weds.frame.common.annotation.OptLog;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/4/4 14:02
 */
@Slf4j
@Component
public class AuthApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final String REQUEST_BEAN_NAME = "requestMappingHandlerMapping";

    @Resource
    private ScBusinessOptApiAuthMapper scBusinessOptApiAuthMapper;
    @Resource
    private ScBusinessOptAuthMapper scBusinessOptAuthMapper;
    @Resource
    private CommUtil commUtil;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 判断RequestMappingHandlerMapping是否存在，若不存在则不扫描
        ApplicationContext applicationContext = event.getApplicationContext();
        if (!applicationContext.containsBean(REQUEST_BEAN_NAME)) {
            log.info("{}不存在, 扫描跳过", REQUEST_BEAN_NAME);
            return;
        }
        log.info("{}存在，开始扫描controller方法", REQUEST_BEAN_NAME);
        // 获取所有的接口方法信息
        RequestMappingHandlerMapping requestMapping =
                applicationContext.getBean(REQUEST_BEAN_NAME, RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> infoMap = requestMapping.getHandlerMethods();

        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : infoMap.entrySet()) {
            HandlerMethod method = entry.getValue();
            String beanType = entry.getValue().getBeanType().getName();
            String methodName = entry.getValue().getMethod().getName();
            OptLog optLog = method.getMethodAnnotation(OptLog.class);
            if (optLog != null) {
                String logValue = optLog.value(); // 业务描述
                String businessNo = optLog.businessNo(); // 业务编码
                if (StringUtils.isBlank(logValue)) { // 获取日志默认描述，未定义时 使用swagger注解描述
                    logValue = method.getMethodAnnotation(ApiOperation.class).value();
                }
                Map logMap = new HashMap();
                logMap.put("logValue", logValue);
                logMap.put("logApi", methodName + "@" + beanType);
                logMap.put("logServer", "base");
                logMap.put("businessNo", businessNo);
                logMap.put("logDescSql", optLog.logDesc());

                ScBusinessOptApi scBusinessOptApi = scBusinessOptApiAuthMapper.selectByApi(methodName + "@" + beanType);
                if (scBusinessOptApi == null) {
                    Integer optId = commUtil.getKeyNextval("sc_business_opt", "opt_id");
                    Integer optApiId = commUtil.getKeyNextval("sc_business_opt_api", "id");
                    logMap.put("optId", optId);
                    logMap.put("optApiId", optApiId);
                    logMap.put("optNo", "OPT" + businessNo + optId);
                    scBusinessOptAuthMapper.insertOrUpdate(logMap);
                    scBusinessOptApiAuthMapper.insertOrUpdate(logMap);
                }
            }
        }
    }
}
